package T0818;

import java.util.Random;
import java.util.Scanner;

/**
 * @date 2025/8/18
 */
public class T1 {
    public static void main(String[] args) {
        t1();
    }

    public static void t1() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入行数：");
        int a = sc.nextInt();
        System.out.print("请输入列数(应当小于行数)：");
        int b = sc.nextInt();
        if (b > a) {
            System.out.println("列数大于行数，错误");
            t1();
        } else {
            for (int i = 1; i <= a; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print("" + i + "*" + "" + j + "=" + (i * j) + "\t");
                }
                System.out.println();
            }
        }
    }

    public static void t2() {
        Scanner sc = new Scanner(System.in);
        int a = (int) (Math.random() * (9001) + (1000));
        int num1 = a / 1000;
        int num2 = a / 100 % 10;
        int num3 = (a % 100) / 10;
        int num4 = ((a % 1000) % 100) % 10;
        String s1 = String.valueOf(a);
        if (num1 == 0) {
            t2();
        } else if (num2 == num3) {
            t2();
        } else if (num2 == num4) {
            t2();
        } else if (num3 == num4) {
            t2();
        } else {
            System.out.println("欢迎来到1A2B猜数字游戏。");
            System.out.println("我已经想好了一个 4 位数字（每位不重复且首位不为零）");
            System.out.println("请试着猜出这个数字。");
            System.out.println("我会用 A 表示数字正确且位置正确，B 表示数字正确但位置错误");
            System.out.println("例如: 如果答案是 1234，你猜 1324，结果是 2A2B");
        }
        int[] nums = new int[4];
        nums[0] = num1;
        nums[1] = num2;
        nums[2] = num3;
        nums[3] = num4;
        while (true) {
            System.out.print("请输入您猜测的数字(首位不为0的四位不重复数字)：");
            int b = sc.nextInt();
            int g1 = b / 1000;
            int g2 = b / 100 % 10;
            int g3 = (b % 100) / 10;
            int g4 = ((b % 1000) % 100) % 10;
            String s2 = String.valueOf(b);
            if (g1 == 0) {
                System.out.println("您输入的数字不是首位不为0的四位不重复数字");
                t2();
            } else if (g2 == g3) {
                System.out.println("您输入的数字不是首位不为0的四位不重复数字");
                t2();
            } else if (g2 == g4) {
                System.out.println("您输入的数字不是首位不为0的四位不重复数字");
                t2();
            } else if (g3 == g4) {
                System.out.println("您输入的数字不是首位不为0的四位不重复数字");
                t2();
            } else {
                int count = 0;
                int x = 0;
                int y = 0;
                for (int i = 0; i < 4; i++) {
                    if (s2.charAt(i) == s1.charAt(i)) {
                        count++;
                        x++;
                    } else {
                        y++;
                    }
                    if (x == 4) {
                        System.out.println("恭喜您，猜对了，您一共猜了" + count + "次");
                        break;
                    } else {
                        System.out.println("猜错了，本次的结果为：" + x + "A" + y + "B");
                        break;
                    }
                }
            }
        }
    }
}
